package com.fyw.xmcamera;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * activity基类
 * Created by FYW on 2017/10/15.
 */

public abstract class BaseActivity extends Activity {
    /**
     * 当前Activity渲染的视图View
     */
    private View mContextView = null;
    /**
     * 是否禁止旋转屏幕
     */
    private boolean isAllowScreenRotate = false;
    /**
     * 日志输出标志
     */
    protected final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        initParam(bundle);

        mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);
        setContentView(mContextView);

        if (!isAllowScreenRotate) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        initView(mContextView);

        doBusiness(this);

        RelativeLayout toolbar = findViewById(R.id.toolbar);
        handleToolbar(new ToolbarHelper(toolbar));
    }

    /**
     * [初始化参数]
     *
     * @param param
     */
    public abstract void initParam(Bundle param);

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int bindLayout();

    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final View view);

    /**
     * [业务操作]
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext);

    /**
     * [简化Toast]
     *
     * @param msg
     */
    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * [是否允许屏幕旋转]
     *
     * @param isAllowScreenRotate
     */
    public void setScreenRotate(boolean isAllowScreenRotate) {
        this.isAllowScreenRotate = isAllowScreenRotate;
    }

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(BaseActivity.this, clz));
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    //----------------------------------------------------------------------------------------------
    protected void handleToolbar(ToolbarHelper toolbarHelper) {
    }

    public static class ToolbarHelper {

        private RelativeLayout mToolbar;

        public ToolbarHelper(RelativeLayout toolbar) {
            this.mToolbar = toolbar;
        }

        public RelativeLayout getToolbar() {
            return mToolbar;
        }

        public void setTitle(String title) {
            TextView titleTV = mToolbar.findViewById(R.id.toolbar_title);
            titleTV.setText(title);
        }

        public ImageView setFinish() {
            ImageView titleTV = mToolbar.findViewById(R.id.toolbar_finish);
            return titleTV;
        }

        public TextView setAdd(){
            TextView titleTV = mToolbar.findViewById(R.id.toolbar_add);
            return titleTV;
        }
    }
}
